package com.api.smoke.task;

import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.zyncro.api.ITaskApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.PagedItemsResultApi;
import com.zyncro.api.model.TaskApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetTasksSmoke extends AbstractApiTest {

	private PagedItemsResultApi<TaskApi> expectedResults;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName() + "1";
		DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		// final String groupUrn = getRepo().getPropertyDto(domainName + "-GroupUrn", new DtoFactory<String>() {
		//
		// public String createDto() {
		//
		// try {
		// ObjectResultApi<String> result = getApi().getService(IGroupApiService.class).createGroup("name", "description", false,
		// false, false, false, false, false, true, null);
		//
		// return result.getObject();
		//
		// } catch (Exception e) {
		//
		// throw new IllegalStateException("Exception creating test data", e);
		// }
		// }
		// });

		String fileName = getRepo().getPropertyDto(domainName + "TasksFile", new DtoFactory<String>() {

			public String createDto() {

				String env = System.getProperty("env");
				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {

					// getApi().getService(ITaskApiService.class).createTask("title", "description", null, "responsibleText", null,
					// TaskApiType.INFORMATION, null, null, groupUrn, null);
					// getApi().getService(ITaskApiService.class).createTask("title2", "description", null, "responsibleText", null,
					// TaskApiType.TASK, null, null, groupUrn, null);
					// getApi().getService(ITaskApiService.class).createTask("title3", "description", null, null, null,
					// TaskApiType.DECISION,
					// null, null, groupUrn, null);

					PagedItemsResultApi<TaskApi> result = getApi().getService(ITaskApiService.class).getTasks(0, 0);

					DataUtil.saveDtoToFile(result, fileName);

				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<PagedItemsResultApi<TaskApi>>() {
		});
	}

	@Test
	public void testGetTask() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;

		PagedItemsResultApi<TaskApi> result = getApi().getService(ITaskApiService.class).getTasks(pageNumber, itemsPerPage);

		Assert.assertEquals(result.getTotalItems(), expectedResults.getTotalItems());
		AssertUtil.assertDTOEquals(result.getElements(), expectedResults.getElements());
	}
}
